<template>
  <div class="pc-login-form">
    <el-form ref="formRef" :model="loginForm" :rules="loginRules" size="large">
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="用户名"
          :prefix-icon="User"
          clearable
          @keyup.enter="handleLogin"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          placeholder="密码"
          :prefix-icon="Lock"
          show-password
          clearable
          @keyup.enter="handleLogin"
        />
      </el-form-item>
      
      <!-- PC特有的记住密码和自动登录选项 -->
      <!-- <div class="login-options">
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        <el-checkbox v-model="loginForm.autoLogin">自动登录</el-checkbox>
      </div> -->

      <!-- 验证码 -->
      <!-- <el-form-item v-if="captchaEnabled" prop="captchaVerification">
        <div class="captcha-container">
          <el-input
            v-model="loginForm.captchaVerification"
            placeholder="请输入验证码"
            style="width: 60%"
            @keyup.enter="handleLogin"
          />
          <div class="captcha-img" @click="refreshCaptcha">
            <img :src="captchaImg" alt="验证码" />
          </div>
        </div>
      </el-form-item> -->

      <el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click="handleLogin">
          登录
        </el-button>
      </el-form-item>

      <!-- PC端特有的其他登录方式 -->
      <!-- <div class="other-login-methods">
        <el-divider>其他登录方式</el-divider>
        <div class="login-icons">
          <el-tooltip content="企业微信登录" placement="bottom">
            <icon-wecom class="login-icon" @click="handleSocialLogin('wechat_enterprise')" />
          </el-tooltip>
          <el-tooltip content="钉钉登录" placement="bottom">
            <icon-dingtalk class="login-icon" @click="handleSocialLogin('dingtalk')" />
          </el-tooltip>
        </div>
      </div> -->
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Lock, User } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const loading = ref(false)
const captchaEnabled = ref(true)
const captchaImg = ref('')

const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false,
  autoLogin: false,
  captchaVerification: ''
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  captchaVerification: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
}

const formRef = ref<FormInstance>()

const handleLogin = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 处理记住密码逻辑
    if (loginForm.rememberMe) {
      localStorage.setItem('rememberedUsername', loginForm.username)
      // 注意：实际项目中密码需要加密处理
      localStorage.setItem('rememberedPassword', loginForm.password)
    } else {
      localStorage.removeItem('rememberedUsername')
      localStorage.removeItem('rememberedPassword')
    }

    // 登录请求
    await userStore.login({
      username: loginForm.username,
      password: loginForm.password,
      captchaVerification: loginForm.captchaVerification
    })

    // 登录成功后的处理
    const redirect = route.query.redirect as string
    router.push(redirect || '/')
    ElMessage.success('登录成功')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

const refreshCaptcha = () => {
  // 刷新验证码的逻辑
  captchaImg.value = `api/captcha?t=${new Date().getTime()}`
}

const handleSocialLogin = (type: string) => {
  // 处理第三方登录逻辑
  console.log('社交登录类型:', type)
}

// 初始化
const initForm = () => {
  // 如果之前记住了密码，则自动填充
  const rememberedUsername = localStorage.getItem('rememberedUsername')
  const rememberedPassword = localStorage.getItem('rememberedPassword')
  if (rememberedUsername && rememberedPassword) {
    loginForm.username = rememberedUsername
    loginForm.password = rememberedPassword
    loginForm.rememberMe = true
  }
  refreshCaptcha()
}

initForm()
</script>

<style lang="scss" scoped>
.pc-login-form {
  width: 100%;
  padding: 20px;

  .login-options {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .captcha-container {
    display: flex;
    gap: 10px;
    align-items: center;

    .captcha-img {
      height: 40px;
      cursor: pointer;
      
      img {
        height: 100%;
      }
    }
  }

  .other-login-methods {
    margin-top: 20px;
    text-align: center;

    .login-icons {
      display: flex;
      justify-content: center;
      gap: 20px;
      margin-top: 15px;

      .login-icon {
        font-size: 24px;
        color: var(--el-text-color-secondary);
        cursor: pointer;
        transition: color 0.3s;

        &:hover {
          color: var(--el-color-primary);
        }
      }
    }
  }
}
</style> 